'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async releasecreat() {
    const { ctx } = this;
    const { uid, theme, themetext, pictureurl } = ctx.request.body;
    if (!(uid && theme && themetext)) {
      return ctx.body = { success: false, msg: '信息不完整!' };
    }
    try {
      const res = await ctx.model.Release.create({
        addTime: Date.now(),
        uid,
        theme,
        themetext,
        workflow: 0,
      });
      const newurl = pictureurl.split(',');
      for (const item of newurl) {
        await ctx.model.Picture.create({
          releaseid: res.releaseid,
          photourl: item,
        });
      }
      ctx.body = { success: true, msg: '发布成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '发布失败', data: e };
    }
  }

  async releaseupdate() {
    const { ctx } = this;
    const { releaseid, theme, themetext, workflow } = ctx.request.body;
    const update = {};
    if (!releaseid) {
      return ctx.body = { success: false, msg: '找不到作品' };
    }
    if (theme) {
      update.theme = theme;
    }
    if (themetext) {
      update.themetext = themetext;
    }
    if (workflow) {
      update.workflow = workflow;
    }
    try {
      await ctx.model.Release.update(
        update,
        {
          where: {
            releaseid,
          },
        }
      );
      ctx.body = { success: true, msg: '修改成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '修改失败' };
    }
  }

  async releasefind() {
    const { ctx } = this;
    const { releaseid, uid, theme, page } = ctx.request.body;
    const where = {};
    const limit = 10;
    const offset = (page - 1) * limit || 0;
    if (releaseid) {
      where.releaseid = releaseid;
    }
    if (uid) {
      where.uid = uid;
    }
    if (theme) {
      where.theme = theme;
    }
    try {
      await ctx.model.Release.hasMany(ctx.model.Picture, {
        foreignKey: 'releaseid',
        sourceKey: 'releaseid',
      });
      await ctx.model.Release.hasMany(ctx.model.Thumbs, {
        foreignKey: 'releaseid',
        sourceKey: 'releaseid',
      });
      await ctx.model.Release.hasMany(ctx.model.Comment, {
        foreignKey: 'releaseid',
        sourceKey: 'releaseid',
      });
      await ctx.model.Release.hasMany(ctx.model.Collection, {
        foreignKey: 'releaseid',
        sourceKey: 'releaseid',
      });
      await ctx.model.Release.hasOne(ctx.model.User, {
        foreignKey: 'uid',
        sourceKey: 'uid',
      });
      const res = await ctx.model.Release.findAll({
        where,
        limit,
        offset,
        include: [{
          model: ctx.model.Picture,
        }, {
          model: ctx.model.Thumbs,
        }, {
          model: ctx.model.Collection,
        }, {
          model: ctx.model.Comment,
        }, {
          model: ctx.model.User,
          attributes: {
            exclude: [ 'pas' ],
          },
        }],
        order: [[ 'releaseid', 'DESC' ]],
      });
      const length = await ctx.model.Release.findAll({
        where,
      });
      ctx.body = { success: true, data: res, length: length.length, maxpage: Math.ceil(length.length / limit) };
    } catch (e) {
      ctx.body = { success: false, msg: '查询失败' };
    }
  }

  async releasevague() {
    const { ctx, app } = this;
    const { Op } = app.Sequelize;
    const { serch, page } = ctx.request.body;
    if (!serch) {
      ctx.body = { success: false, msg: '请输入查询内容' };
    }
    const limit = 10;
    const offset = (page - 1) * limit || 0;
    try {
      const res = await ctx.model.Release.findAndCountAll({
        where: {
          theme: {
            [Op.like]: `%${serch}%`,
          },
        },
        limit,
        offset,
      });
      for (const item of res.rows) {
        const pic = await ctx.model.Picture.findAll({
          where: {
            releaseid: item.releaseid,
          },
        });
        item.setDataValue('picture', pic);
      }
      ctx.body = { success: true, data: res };
    } catch (e) {
      ctx.body = { success: false, msg: '查询失败', data: e };
    }
  }

  async releasedelete() {
    const { ctx } = this;
    const { releaseid } = ctx.request.body;
    if (!releaseid) {
      ctx.body = { success: false, msg: '请传入指定参数' };
    }
    try {
      await ctx.model.Release.destroy({
        where: {
          releaseid,
        },
      });
      ctx.body = { success: true, msg: '删除成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '删除失败' };
    }
  }

  async releaselike() {
    const { ctx, app } = this;
    const { Op } = app.Sequelize;
    const { search, page } = ctx.request.body;
    const limit = 10;
    const offset = (page - 1) * limit || 0;
    try {
      await ctx.model.Release.hasMany(ctx.model.Picture, {
        foreignKey: 'releaseid',
        sourceKey: 'releaseid',
      });
      await ctx.model.Release.hasMany(ctx.model.Thumbs, {
        foreignKey: 'releaseid',
        sourceKey: 'releaseid',
      });
      await ctx.model.Release.hasMany(ctx.model.Comment, {
        foreignKey: 'releaseid',
        sourceKey: 'releaseid',
      });
      await ctx.model.Release.hasMany(ctx.model.Collection, {
        foreignKey: 'releaseid',
        sourceKey: 'releaseid',
      });
      await ctx.model.Release.hasOne(ctx.model.User, {
        foreignKey: 'uid',
        sourceKey: 'uid',
      });
      const res = await ctx.model.Release.findAll({
        where: {
          [Op.or]: [
            {
              theme: {
                [Op.like]: `%${search}%`,
              },
            },
            {
              themetext: {
                [Op.like]: `%${search}%`,
              },
            },
          ],
        },
        limit,
        offset,
        include: [{
          model: ctx.model.Picture,
        }, {
          model: ctx.model.Thumbs,
        }, {
          model: ctx.model.Collection,
        }, {
          model: ctx.model.Comment,
        }, {
          model: ctx.model.User,
          attributes: {
            exclude: [ 'pas' ],
          },
        }],
        order: [[ 'releaseid', 'DESC' ]],
      });
      const length = await ctx.model.Release.findAll({
        where: {
          [Op.or]: [
            {
              theme: {
                [Op.like]: `%${search}%`,
              },
            },
            {
              themetext: {
                [Op.like]: `%${search}%`,
              },
            },
          ],
        },
      });
      ctx.body = { success: true, data: res, length: length.length, maxpage: Math.ceil(length.length / limit) };
    } catch (e) {
      ctx.body = { success: false, msg: '查询失败' };
    }
  }
}

module.exports = HomeController;
